<?php

class StoreController extends Controller
{	
	public function init(){
		parent::init();
		$cs = Yii::app()->clientScript;
		$cs->registerScriptFile('/js/facebox.js');
		$cs->registerScriptFile('/js/tiny_mce/tiny_mce.js');
		$cs->registerScriptFile('/js/highcharts.js');
		$cs->registerScriptFile('/js/jquery.sharrre-1.3.2.min');
	}
	
	public function actionIndex($name, $key='todos')
	{	
		$store = Store::model()->findByAttributes(array('name'=>$name));
		$products = Product::model()->allProductsByStore($store->id, $key);
		$types = Type::model()->findAll();
		$this->render('index', array('store'=>$store, 'products'=>$products, 'types'=>$types));
	}
	
	public function actionSearch()
	{	
		$id = $_POST['id'];
		$key = $_POST['key'];
		$products = Product::model()->allProductsByStore($id, $key);
		$types = Type::model()->findAll();
		$this->renderPartial('_products', array('products'=>$products, 'types'=>$types));
		Yii::app()->end();
	}
	
	public function actionEdit($name){
		$store = Store::model()->findByAttributes(array('name'=>$name));
		$old = $store->logo_url;
		if(isset($_POST['Store']))
		{
			$store->attributes = $_POST['Store'];
			$store->logo_url = CUploadedFile::getInstance($store,'logo_url');
			if(is_object($store->logo_url)){
				$file_name = $store->logo_url->getName();
			}
			else
				$store->logo_url = $old;
			if($store->save())
			{
				if(is_object($store->logo_url)){
					$path = 'images/marcas/'.$store->id.'/';
					$store->logo_url->saveAs($path.'big_'.$store->logo_url->getName());
					$image = Yii::app()->image->load($path.'big_'.$store->logo_url->getName());
					$image->resize(120,40);
					$image->save($path.$store->logo_url->getName());
				}
				
				$this->redirect(array('index','name'=>$store->name));
			}
		}
		$this->renderPartial('_edit', array('store'=>$store));
	}
	public function actionNew()
	{
		$this->layout='//layouts/withchat';
    $model=new Store;
    $user=new User;
    
    if(isset($_POST['Store']))
    {	
    	$user->attributes=$_POST['User'];
    	$user->password=MD5($user->password);
      if($user->save())
      {	
      	$model->attributes=$_POST['Store'];
      	$model->user_id = $user->id;
      	$model->visit_price = $model->new_visit_price;
      	$model->logo_url = CUploadedFile::getInstance($model,'logo_url');
        
      	if($model->save()){
      		$path = 'images/marcas/'.$model->id.'/';
					mkdir($path);
      		$model->logo_url->saveAs($path.'big_'.$model->logo_url->getName());
					$image = Yii::app()->image->load($path.'big_'.$model->logo_url->getName());
					$image->resize(120,40);
					$image->save($path.$model->logo_url->getName());
					
					$this->redirect(array('site/login'));
      	}
      }
    }
    $this->render('new',array('model'=>$model, 'user'=>$user));
	}
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}